import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex) // vue的插件机制

// Vuex.Store 构造器选项
const store = new Vuex.Store({
	// 为了不和页面或组件的data中的造成混淆，state中的变量前面建议加上$符号
	state: {
		// 用户信息
		//       $userInfo: {
		//           id: 1
		//       },
		token:''
	},
	getters: {
		$userInfo: state => {
			
			// return state.$userInfo
			      if (!state.$userInfo) {
			        try {
			          const t = JSON.parse(uni.getStorageSync("$userinfo"))
			          state.$userInfo = t
			        } catch (e) {
			          console.error(e)
			        }
			      }
			      return state.$userInfo
		},
		$token: state => {
			// return state.$token
			      if (!state.$token) {
			        try {
			          const t = uni.getStorageSync("$token")
			          state.$token = t
			        } catch (e) {
			          console.error(e)
			        }
			      }
			      return state.$token
			// return uni.getStorageSync("$token")
		}
	},
	mutations: {
		setToken(state, value) {
			uni.setStorageSync("$token",value)
			state.$token = value
		},
		setUserInfo(state, value) {
			uni.setStorageSync("$userinfo",JSON.stringify(value))
			state.$userInfo = value
		},
		logout(state,value)
		{
			state.change=value
			uni.removeStorageSync("$userinfo")
			uni.removeStorageSync("$token")
			state.$token=null
			state.$userInfo=null
			uni.navigateTo({
				url: "../login/index"
			})
		}
	}
})

export default store
